package com.jams.goodox.activity.serve;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.widget.RelativeLayout;

import com.hangzhou.jin.customview.LoadView;
import com.jams.goodox.BaseActivity;
import com.jams.goodox.R;
import com.jams.goodox.TownApplicationLike;
import com.jams.goodox.constant.AppConfig;
import com.jams.goodox.entity.BuildBean;
import com.jams.goodox.entity.resp.SurroundingListResp;
import com.jams.goodox.net.RetrofitCallbackListener;
import com.jams.goodox.net.services.LifeService;
import com.jams.goodox.utils.CollectionUtils;
import com.jams.goodox.utils.DelayUtils;
import com.jams.goodox.utils.DensityUtil;
import com.jams.goodox.utils.StyleUtil;
import com.jams.goodox.utils.ViewUtils;
import com.jams.goodox.widget.RefreshRecyclerView;
import com.jams.goodox.widget.mannger.RefreshLinearLayoutManager;
import com.jams.library.commonAdapter.RecycleCommonAdapter;
import com.jams.library.commonAdapter.RecycleCommonViewHolder;
import com.jams.library.listener.OnRecycleItemClickListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import retrofit2.Call;

/**
 * 周边楼盘列表
 *
 * create by 顾佳佳 on 2017/05/22
 */

public class SurroundingActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    @Bind(R.id.rcv_surrounding)
    RefreshRecyclerView rcvSurrounding;
    @Bind(R.id.srl_surrounding)
    SwipeRefreshLayout srlSurrounding;
    @Bind(R.id.empty_view)
    LoadView emptyView;

    private int curPage;
    private List<BuildBean> dataList;
    private float height;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_surrounding;
    }

    @Override
    protected void init() {
        srlSurrounding.setOnRefreshListener(this);
        dataList = new ArrayList<>();
        StyleUtil.titleBackKey(this,"周边楼盘");
        rcvSurrounding.setLayoutManager(new RefreshLinearLayoutManager(this));
        height = ViewUtils.getCompressHeight(280,608, TownApplicationLike.width - DensityUtil.dp2px(getContext(), 16));
        rcvSurrounding.setAdapter(new RecycleCommonAdapter<BuildBean>(this,dataList,R.layout.surrounding_item) {
            @Override
            public void convert(RecycleCommonViewHolder helper, final BuildBean item, int position) {
                ViewUtils.setRelativeLayoutWH(helper.getView(R.id.img_icon), RelativeLayout.LayoutParams.MATCH_PARENT,(int)height);
                helper.setSimpleDraweeView(R.id.img_icon,item.getBuildingPic());
                helper.setText(R.id.tv_build_name,item.getBuildingName()).
                        setOnItemClickListener(new OnRecycleItemClickListener() {
                    @Override
                    public void itemClick() {
                        Intent intent = new Intent(SurroundingActivity.this,SurroundingDetailActivity.class);
                        intent.putExtra(SurroundingDetailActivity.BUILD,item);
                        qStartActivity(intent);
                    }
                });
            }
        });
        rcvSurrounding.setLoadDataListener(new RefreshRecyclerView.LoadDataListener() {
            @Override
            public void onLoadMore() {
                getSurroundingList();
            }
        });
        onRefresh();
    }

    @Override
    public void onRefresh() {
        DelayUtils.doAfterDelay(100, new DelayUtils.DelayDoListener() {
            @Override
            public void doAction() {
                srlSurrounding.setRefreshing(true);
                curPage = 1;
                getSurroundingList();
            }
        });
    }

    private void getSurroundingList() {
        enqueue(createCallService(LifeService.class).getSurroundingList(curPage, AppConfig.PagSize), new RetrofitCallbackListener<SurroundingListResp>() {
            @Override
            public void onSuccess(Call<SurroundingListResp> call, SurroundingListResp result) {
                srlSurrounding.setRefreshing(false);
                rcvSurrounding.loadMoreComplete();
                if (result.getData() != null) {
                    if (curPage == 1) {
                        dataList.clear();
                    }
                    if (result.getData().getPageInfo() == null) {
                        rcvSurrounding.setCanLoadMore(false);
                    } else {
                        rcvSurrounding.setCanLoadMore(result.getData().getPageInfo().isHasMore());
                    }
                    dataList.addAll(result.getData().getResultList());
                    curPage++;
                    rcvSurrounding.notifyData();
                }
                if (!CollectionUtils.hasData(dataList)) {
                    emptyView.loadCompleteNoData(R.mipmap.holder_no_common,"暂无相关内容");
                } else {
                    emptyView.loadComplete();
                }

            }

            @Override
            public void onError(String msg) {
                if (!CollectionUtils.hasData(dataList)) {
                    emptyView.loadCompleteNoData(R.mipmap.holder_no_common,"暂无相关内容");
                } else {
                    emptyView.loadComplete();
                }
                srlSurrounding.setRefreshing(false);
                rcvSurrounding.loadMoreComplete();
            }
        });
    }
}
